package com.taobao.pamirs.schedule.conf;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.taobao.pamirs.schedule.strategy.TBScheduleManagerFactory;
import com.taobao.pamirs.schedule.zk.ZKManager.keys;

@Configuration
@ConditionalOnProperty(prefix = "tbschedule", name = "enabled", havingValue = "true")
@EnableConfigurationProperties({ TBScheduleProperties.class })
public class TBScheduleConfig {
	@Autowired
	TBScheduleProperties tBScheduleProperties;

	@Bean(initMethod = "init")
	public TBScheduleManagerFactory tbScheduleManagerFactory() {
		TBScheduleManagerFactory tbScheduleManagerFactory = new TBScheduleManagerFactory();
		Map<String, String> zkConfig = new HashMap<String, String>();
		zkConfig.put(keys.zkConnectString.toString(), tBScheduleProperties.getZkConnectString());
		zkConfig.put(keys.rootPath.toString(), tBScheduleProperties.getRootPath());
		zkConfig.put(keys.zkSessionTimeout.toString(), tBScheduleProperties.getZkSessionTimeout());
		zkConfig.put(keys.userName.toString(), tBScheduleProperties.getUsername());
		zkConfig.put(keys.password.toString(), tBScheduleProperties.getPassword());
		zkConfig.put(keys.isCheckParentPath.toString(), tBScheduleProperties.getIsCheckParentPath());
		tbScheduleManagerFactory.setZkConfig(zkConfig);
		return tbScheduleManagerFactory;
	}

}
